package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.GoodsCreateAndUpdateBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketAdmin;
import com.cskaoyan.bean.pojo.MarketGoods;

import com.cskaoyan.bean.vo.CatAndBrandVO;

import com.cskaoyan.bean.vo.GoodsDetailVO;

import com.cskaoyan.bean.vo.GoodsListVO;
import com.cskaoyan.service.GoodsService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 商品管理-商品管理
 * 创建日期: 2022/04/07 14:27
 *
 * @author HanWeiqi
 */
@RequestMapping("admin/goods")
@RestController
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    // 商品查询
    @RequestMapping("list")
    public BaseRespVo goodsList(BaseParam baseParam,Integer goodsId,String goodsSn,String name){
        GoodsListVO goodsListVO = goodsService.goodsList(baseParam,goodsId,goodsSn,name);
        return BaseRespVo.ok(goodsListVO);
    }

    // 商品品牌查看
    @RequestMapping("catAndBrand")
    public BaseRespVo catAndBrand() {
        CatAndBrandVO catAndBrandVO = goodsService.catAndBrand();

        return BaseRespVo.ok(catAndBrandVO);
    }



    // 商品详情查询
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id){
        GoodsDetailVO goodsDetailVO = goodsService.detail(id);
        return BaseRespVo.ok(goodsDetailVO);
    }

    // 商品新建

    @PostMapping("create")
    public BaseRespVo goodsCreate(@RequestBody @Valid GoodsCreateAndUpdateBO createBO, BindingResult result){

        if(result.hasFieldErrors()){
            return BaseRespVo.fail(401, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }

        goodsService.goodsCreate(createBO);
        return BaseRespVo.ok();

    }

    // 商品修改
    @RequestMapping("update")
    public BaseRespVo goodsUpdate(@RequestBody @Valid GoodsCreateAndUpdateBO createBO, BindingResult result){

        if(result.hasFieldErrors()){
            return BaseRespVo.fail(401, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        }
        goodsService.goodsUpdate(createBO);
        return BaseRespVo.ok();
    }


    // 商品虚假删除
    @RequestMapping("delete")
    public BaseRespVo goodsDelete(@RequestBody MarketGoods marketGoods){
        int count = goodsService.goodsDelete(marketGoods);
        if(count == 1){
            return BaseRespVo.ok();
        }else{
            return BaseRespVo.fail(10000,"删除失败");
        }
    }
}
